import Home from '../home/home-model.js';
import config from '../utils/config.js'
const homeModel = new Home();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    userInfo: {},
    collections: [],
    actionSheetHidden: true

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    if (!wx.getStorageSync('userInfo')) {
      this.setData({
        actionSheetHidden: false
      });
    }

  },
  logout() {
    wx.removeStorageSync('userInfo');
  },
  onAboutItem() {
    wx.navigateTo({
      url: '../about/about',
    })
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    this.setData({
      userInfo: wx.getStorageSync('userInfo')
    })
    var collections = wx.getStorageSync('collections');
    var newsDetail = []
    if (collections) {
      collections.map((item, index) => {
        item.newsDetail.image = config.imgBaseUrl + item.newsDetail.image;
        newsDetail.push(item.newsDetail)
      })
    }
    this.setData({
      collections: newsDetail
    })
  },
  onNewsDetail(e) {
    var id = homeModel.getDataSet(e, 'index');
    wx.navigateTo({
      url: '../newsDetail/newsDetail?id=' + id,
    });
  },
  //授权登陆
  onGotUserInfo(e) {
    var userInfo = e.detail.userInfo;
    if (userInfo) {
      homeModel.login(userInfo, (res) => {
        if (res.status === 1) {
          wx.setStorageSync('userInfo', res.data);
          this.setData({
            userInfo: userInfo
          })
          wx.showToast({
            title: '登陆成功',
          })
        } else {
          wx.showToast({
            title: '登陆失败',
          })
        }
      });
    }
    this.setData({
      actionSheetHidden: true
    })
  },
  //登陆开关
  actionSheetChange() {
    this.setData({
      actionSheetHidden: true
    })
  },
  register() {
    this.setData({
      actionSheetHidden: false
    })
  },


  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})